package cn.xtits.design.controller;
import cn.xtits.design.entity.TemplateData;
import cn.xtits.design.entity.TemplateDataExample;
import cn.xtits.design.entity.Pagination;
import cn.xtits.design.service.TemplateDataService;
import cn.xtits.xtf.common.web.AjaxResult;
import cn.xtits.xtf.common.utils.JsonUtil;
import java.math.BigDecimal;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.joda.time.DateTime;
import org.joda.time.format.DateTimeFormat;
import org.joda.time.format.DateTimeFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;

/**
* @fileName: TemplateDataController.java
* @author: Dan
* @createDate: 2019-10-23 14:00:06
* @description: 模版数据
*/
@RestController
@RequestMapping("/templateData")
public class TemplateDataController extends BaseController {

    @Autowired
    private TemplateDataService service;

    //@RequiresPermissions("templatedata:insert")
    @RequestMapping(value = "insertTemplateData")
        public AjaxResult insertTemplateData(
                @RequestParam(value = "data", required = false) String data) {
        TemplateData record = JsonUtil.fromJson(data, TemplateData.class);
        //Date dt = getDateNow();
        record.setCreateDate(null);
        record.setMakeBillMan(getUserName());
        record.setModifier(getUserName());
        record.setModifyDate(null);
        record.setDeleteFlag(false);
        service.insert(record);
        return new AjaxResult(record);
    }

    //@RequiresPermissions("templatedata:delete")
    @RequestMapping(value = "deleteTemplateData")
    public AjaxResult deleteTemplateData(
            @RequestParam(value = "id", required = false) int id) {
        TemplateData record = new TemplateData();
        record.setId(id);
        record.setDeleteFlag(true);
        record.setModifier(getUserName());
        record.setModifyDate(null);
        int row = service.updateByPrimaryKeySelective(record);
        return new AjaxResult(row);
    }

    //@RequiresPermissions("templatedata:update")
    @RequestMapping(value = "updateTemplateData")
    public AjaxResult updateTemplateData(
            @RequestParam(value = "data", required = false) String data) {
        TemplateData record = JsonUtil.fromJson(data, TemplateData.class);
        record.setCreateDate(null);
        record.setMakeBillMan(null);
        record.setModifyDate(null);
        record.setModifier(getUserName());
        record.setDeleteFlag(false);
        service.updateByPrimaryKeySelective(record);
        return new AjaxResult(record);
    }

    @RequestMapping(value = "listTemplateData")
    public AjaxResult listTemplateData(
            @RequestParam(value = "templateId", required = false) String templateId,
            @RequestParam(value = "templateName", required = false) String templateName,
            @RequestParam(value = "templateContent", required = false) String templateContent,
            @RequestParam(value = "templateData", required = false) String templateData,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "pageIndex", required = false) Integer pageIndex,
            @RequestParam(value = "orderBy", required = false) String orderBy,
            @RequestParam(value = "startDate", required = false) String startDate,
            @RequestParam(value = "endDate", required = false) String endDate) {
        DateTimeFormatter format = DateTimeFormat.forPattern("yyyy-MM-dd HH:mm:ss");
        TemplateDataExample example = new TemplateDataExample();
        example.setPageIndex(pageIndex);
        example.setPageSize(pageSize);
        if (StringUtils.isNotBlank(orderBy)) {
            example.setOrderByClause(orderBy);
        }
        TemplateDataExample.Criteria criteria = example.createCriteria();
        criteria.andDeleteFlagEqualTo(false);

        if (StringUtils.isNotBlank(templateId)) {
            String[] split = templateId.split(",");
            if (split.length > 1) {
                List<Integer> templateIdList = new ArrayList<>();
                for (String s : split) {
                        templateIdList.add(Integer.parseInt(s));
                }
                criteria.andTemplateIdIn(templateIdList);
            } else {
                criteria.andTemplateIdEqualTo(Integer.parseInt(split[0]));
            }
        }

        if (StringUtils.isNotBlank(templateName)) {
            criteria.andTemplateNameLike(templateName);
        }
        if (StringUtils.isNotBlank(templateContent)) {
            criteria.andTemplateContentLike(templateContent);
        }
        if (StringUtils.isNotBlank(templateData)) {
            criteria.andTemplateDataLike(templateData);
        }
        if (StringUtils.isNotBlank(startDate)) {
            criteria.andCreateDateGreaterThanOrEqualTo(DateTime.parse(startDate, format).toDate());
        }
        if (StringUtils.isNotBlank(endDate)) {

            criteria.andCreateDateLessThanOrEqualTo(DateTime.parse(endDate, format).toDate());
        }

        List<TemplateData> list = service.listByExample(example);
        Pagination<TemplateData> pList = new Pagination<>(example,list,example.getCount());
        return new AjaxResult(pList);
    }

    @RequestMapping(value = "getTemplateData")
    public AjaxResult getTemplateData(
            @RequestParam(value = "id", required = false) Integer id) {
        TemplateData res = service.getByPrimaryKey(id);
        return new AjaxResult(res);
    }

}